package com.lge.clock;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lge.clock.stopwatch.LapValue;
import com.lge.clock.stopwatch.StopWatchAdapter;

/**
 * @author lge.com
 */
public class StopWatchFragment extends Fragment implements OnClickListener
{
	private static final String TAG = "StopWatchActivity";
	private static final int DEFAULT_BUTTON_STATE = 0;
	private static final int START_BUTTON_STATE = 1;
	private static final int STOP_BUTTON_STATE = 2;
	
	private static final int LAPCOUNT = 99;
	private static final String STOPWATCHSTARTED = "stopwatchstarted";
	private static final String STARTTIME = "starttime";
	private static final String ENDTIME = "endtime";
	
	private LinearLayout sw_bg_linearLayout;
	
	private TextView mHourTextView;
	private TextView mMinuteTextView;
	private TextView mSecondTextView;
	private TextView mCentiSecondTextView;
	
	private ListView sw_listview;
	private Button sw_start_btn;
	private Button sw_stop_btn;
	private Button sw_lap_btn;
	private Button sw_reset_btn;
	private Button sw_resume_btn;
	private Timer mTimer;

	private long time;
	private String mHourStr;
	private String mMinuteStr;
	private String mSecondStr;
	private String mCentiSecondStr;
	
	private int lap_count;
	
	private long mStartTime = 0;
	private long mFlowTime = 0;
	
	private boolean bStartTimer = false;
	
	private List<LapValue> lap_list;
	private StopWatchAdapter swAdapter;
	
    private PowerManager mPm;
    private WakeLock mWakeLock;

	private static SharedPreferences mSharedPreferences;
	private static SharedPreferences.Editor mEditor;

	//[mo2sangbong.lee][1647] 2011.11.02 START
	private IntentFilter mIntentFilter;
	//[mo2sangbong.lee][1647] 2011.11.02 END
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.stopwatch_main, container ,false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		init();
	}

	@Override
	public void onStart() {
//		init();
		if (time == 0L){
			clearList();
		}
		super.onStart();
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mSharedPreferences = getActivity().getSharedPreferences("STOPWATCH_STATE", 0);
		mEditor = mSharedPreferences.edit();
		if(loadIsStopWatchStartedPreferences()==-1){
			saveIsStopWatchStartedPreferences(DEFAULT_BUTTON_STATE);	
		}
		setHasOptionsMenu(true);
		
		
	}
	/*
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.stopwatch_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
			case R.id.stopwatch_start_menu:
			{
				Log.d(TAG, "[onClick] : click the start button");
				startTimer(System.currentTimeMillis());
				saveIsStopWatchStartedPreferences(START_BUTTON_STATE);
				setButtonState(START_BUTTON_STATE);
				save_lab_list();
				//[hyunjung78.park] 2011.08.04 Apply GUI
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_start);
				break;
			}
		case R.id.stopwatch_stop_menu:
			{
				Log.d(TAG, "[onClick] : click the stop button");
				mFlowTime = time;
				saveStopTimePreferences(time);
				stopTimer();
				saveIsStopWatchStartedPreferences(STOP_BUTTON_STATE);
				setButtonState(STOP_BUTTON_STATE);
				save_lab_list();
				//[hyunjung78.park] 2011.08.04 Apply GUI
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_stop);
				if (mWakeLock.isHeld()) 
				{
					mWakeLock.release();
				}
				break;
			}
		case R.id.stopwatch_lab_menu:
		{
			Log.d(TAG, "[onClick] : click the lap button");
			addList();
			save_lab_list();
			break;
		}
		case R.id.stopwatch_resume_menu:
		{
			Log.d(TAG, "[onClick] : click the resume button");
			//[hyunjung78.park][19119] 2011.08.05 Fixed issue that backlight is off START
			if(mWakeLock == null)
			{
				mWakeLock = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "dim_Wake");
				mWakeLock.acquire();	
			}else{
				mWakeLock.acquire();
			}
			//[hyunjung78.park][19119] 2011.08.05 Fixed issue that backlight is off END
			startTimer(System.currentTimeMillis() - mFlowTime);
			saveIsStopWatchStartedPreferences(START_BUTTON_STATE);
			//[hyunjung78.park] 2011.08.04 Apply GUI
			//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_start);
			setButtonState(START_BUTTON_STATE);
			save_lab_list();
			break;
		}
		case R.id.stopwatch_reset_menu:
		{
			Log.d(TAG, "[onClick] : click the reset button");
			stopTimer();
			clearList();
							
			mHourTextView.setText(R.string.sw_defaultvalue2);
			mMinuteTextView.setText(R.string.sw_defaultvalue2);
			mSecondTextView.setText(R.string.sw_defaultvalue2);
			mCentiSecondTextView.setText(R.string.sw_defaultvalue);
			saveIsStopWatchStartedPreferences(DEFAULT_BUTTON_STATE);
			//[hyunjung78.park] 2011.08.04 Apply GUI
			//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_rap);
			
			setButtonState(DEFAULT_BUTTON_STATE);
			if (mWakeLock.isHeld()) 
			{
				mWakeLock.release();
			}
			save_lab_list();
			break;
		}
		}
		getActivity().invalidateOptionsMenu();
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		final int isStopWatchStarted = loadIsStopWatchStartedPreferences();
		MenuItem mi;
		mi = menu.findItem(R.id.stopwatch_start_menu);
		mi.setVisible(isStopWatchStarted == DEFAULT_BUTTON_STATE);
		mi = menu.findItem(R.id.stopwatch_stop_menu);
		mi.setVisible(isStopWatchStarted == START_BUTTON_STATE);
		mi = menu.findItem(R.id.stopwatch_lab_menu);
		mi.setVisible(isStopWatchStarted == START_BUTTON_STATE);
		mi = menu.findItem(R.id.stopwatch_resume_menu);
		mi.setVisible(isStopWatchStarted == STOP_BUTTON_STATE);
		mi = menu.findItem(R.id.stopwatch_reset_menu);
		mi.setVisible(isStopWatchStarted == STOP_BUTTON_STATE);
	}
*/
	@Override
	public void onResume() 
	{
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(TAG, "[onResume]");
		lap_list.clear();
		restore_lab_list();
		int isStopWatchStarted = loadIsStopWatchStartedPreferences();


		if (START_BUTTON_STATE == isStopWatchStarted)
		{
			Long time = loadStartTimePreferences();
			stopTimer();//P930_TD#13400_20110720 Fixed issue after change settings date&time
			startTimer(time);
			setButtonState(START_BUTTON_STATE);
			/*
			//[hyunjung78.park][19119] 2011.08.05 Fixed issue that backlight is off START
			if(mWakeLock == null)
			{
				mWakeLock = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "dim_Wake");
				mWakeLock.acquire();	
			}else{
				mWakeLock.acquire();
			}
			*/
		}
		else if(STOP_BUTTON_STATE == isStopWatchStarted)
		{
			Long time = loadStopTimePreferences();
			mFlowTime = time;
			
			//[START]P930_TD#13400_20110720 Fixed issue after change settings date&time
            mHourStr = getTwoLengthNum(((time/1000)/60)/60);
			mMinuteStr = getTwoLengthNum((time/60000)%60);
			mSecondStr = getTwoLengthNum((time/1000)%60);
			//[END]P930_TD#13400_20110720 Fixed issue after change settings date&time
			mCentiSecondStr = Long.toString((time/100)%10);
			mHourTextView.setText(mHourStr);
			mMinuteTextView.setText(mMinuteStr);
			mSecondTextView.setText(mSecondStr);
			mCentiSecondTextView.setText(mCentiSecondStr);
			setButtonState(STOP_BUTTON_STATE);
		}
		else if (DEFAULT_BUTTON_STATE == isStopWatchStarted) 
		{
			stopTimer();
			clearList();				
			setButtonState(DEFAULT_BUTTON_STATE);
		}
	}

	@Override
	public void onPause()
	{
		super.onPause();
		Log.d(TAG, "[onPause]");
		save_lab_list();
		
		if (mWakeLock != null && mWakeLock.isHeld()) 
		{
			mWakeLock.release();
		}
//		 save_lab_list();
	}

	@Override
	public void onStop() 
	{
		super.onStop();
		Log.d(TAG, "[onStop]");
		//[mo2sangbong.lee][1647] 2011.11.02 START
		stopTimer();
		//[mo2sangbong.lee][1647] 2011.11.02 END
		
	}

	private void save_lab_list() {
		String count;
		String time1;
		String time2;
		for (int i = 0; i < lap_list.size(); i++) {
			Log.d(TAG, "LAPLISTSIZE====>" + lap_list.size());
			LapValue lv = lap_list.get(i);
			count = lv.getLap_count();
			time1 = lv.getLap_time1();
			time2 = lv.getLap_time2();
			mEditor.remove(count);
			mEditor.remove(time1);
			mEditor.remove(time2);
			// mEditor.putInt("index"+i, i);
			mEditor.putString("lap_count" + i, count);
			Log.d(TAG, "LAPCOUNT====>" + count);
			mEditor.putString("lap_time1" + i, time1);
			Log.d(TAG, "LAPTIME1====>" + time1);
			mEditor.putString("lap_time2" + i, time2);
			Log.d(TAG, "LAPTIME2====>" + time2);
		}
		mEditor.putInt("lap_num", lap_list.size());
		Log.d(TAG, "LAP_NUM====>" + lap_list.size());
		mEditor.commit();
	}

	private void restore_lab_list() {
		int lap_num = mSharedPreferences.getInt("lap_num", 0);
		if (lap_num != 0) {
			for (int i = lap_num; i > 0; --i) {
				// String count = mSharedPreferences.getString("lap_count"+i,
				// "");
				String count = Integer.toString(lap_num + 1 - i);
				String time1 = mSharedPreferences.getString("lap_time1"+ (i - 1), "");
				String time2 = mSharedPreferences.getString("lap_time2"+ (i - 1), "");
				lap_list.add(0, new LapValue(count, time1, time2));
			}
			swAdapter.notifyDataSetChanged();
		}
		if (lap_num == 100) {
			sw_lap_btn.setEnabled(false);
			sw_lap_btn.setTextColor(getResources().getColor(R.color.lap_button_disable));
			sw_lap_btn.setAlpha(0.4f);
		}
	}

	@Override
	public void onDestroy() 
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		
		Log.d(TAG, "[onDestroy]");
//			save_lab_list();

		//[mo2sangbong.lee][1647] 2011.11.02 START
		if(mBroadCastReceiver != null)
		{	
			try{
				this.getActivity().unregisterReceiver(mBroadCastReceiver);
			}catch(Exception e){
				e.printStackTrace();
			}

			mBroadCastReceiver = null;
		}
		//[mo2sangbong.lee][1647] 2011.11.02 END
	}
	
	private void init()
	{
		Log.d(TAG, "[init] : initialize");
        mPm = (PowerManager)getActivity().getSystemService(Context.POWER_SERVICE);
        mWakeLock = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "dim_Wake");
        
        //[suhaeng.lee][6765] 2012.05.03 to use only blue background color
        //sw_bg_linearLayout = (LinearLayout)getView().findViewById(R.id.sw_bg_rap);
        
        //[mo2sangbong.lee][1647] 2011.11.02 START        
		mIntentFilter = new IntentFilter();
		mIntentFilter.addAction("android.intent.action.TIME_SET");
		this.getActivity().registerReceiver(mBroadCastReceiver, mIntentFilter);
		//[mo2sangbong.lee][1647] 2011.11.02 END
		
		//[hyunjung78.park] 2011.08.04 Apply GUI
		mHourTextView = (TextView)getView().findViewById(R.id.sw1_textview);
		mMinuteTextView = (TextView)getView().findViewById(R.id.sw2_textview);
		mSecondTextView = (TextView)getView().findViewById(R.id.sw3_textview);
		mCentiSecondTextView = (TextView)getView().findViewById(R.id.sw4_textview);
		
		//added by essin@lge.com 2010.12.31 because font issue!
		mHourTextView.setTypeface(Typeface.createFromFile("/system/fonts/Roboto-Regular.ttf"), 0);
		mMinuteTextView.setTypeface(Typeface.createFromFile("/system/fonts/Roboto-Regular.ttf"), 0);
		mSecondTextView.setTypeface(Typeface.createFromFile("/system/fonts/Roboto-Regular.ttf"), 0);
		mCentiSecondTextView.setTypeface(Typeface.createFromFile("/system/fonts/Roboto-Regular.ttf"), 0);
		
		sw_listview = (ListView)getView().findViewById(R.id.sw_list);
		
		sw_start_btn = (Button)getView().findViewById(R.id.sw_start_btn);
		sw_start_btn.setOnClickListener(this);
		sw_stop_btn = (Button)getView().findViewById(R.id.sw_stop_btn);
		sw_stop_btn.setOnClickListener(this);
		sw_lap_btn = (Button)getView().findViewById(R.id.sw_lap_btn);
		sw_lap_btn.setOnClickListener(this);
		sw_reset_btn = (Button)getView().findViewById(R.id.sw_reset_btn);
		sw_reset_btn.setOnClickListener(this);
		sw_resume_btn = (Button)getView().findViewById(R.id.sw_resume_btn);
		sw_resume_btn.setOnClickListener(this);
	
		lap_list = new ArrayList<LapValue>();
		swAdapter = new StopWatchAdapter(getActivity(), R.layout.stopwatch_row, lap_list);
		sw_listview.setAdapter(swAdapter);
		mSharedPreferences = this.getActivity().getSharedPreferences("STOPWATCH_STATE", 0);
		mEditor = mSharedPreferences.edit();
		
		}

	



	private void updateArray(){
		lap_list.clear();
		sw_listview.setAdapter(swAdapter);//P930_TD#13400_20110720 Fixed issue after change settings date&time
	}
	private void startTimer(long startTime)
	{
		Log.d(TAG, "[startTimer] : start the stopwatch");
		
		//[hyunjung78.park] 2011.10.04 change the code location for backlight on issue 
		//[hyunjung78.park][19119] 2011.08.05 Fixed issue that backlight is off START
		if(mWakeLock == null)
		{
			mWakeLock = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "dim_Wake");
			mWakeLock.acquire();	
		}else{
			mWakeLock.acquire();
		}
		//[hyunjung78.park][19119] 2011.08.05 Fixed issue that backlight is off END
		
		if (bStartTimer == true) {
			Log.e(TAG, "[startTimer] : startTimer is already started");
			return;
		}
		bStartTimer = true;
		
		saveStartTimePreferences(startTime);
		mStartTime = startTime;
		mTimer = new Timer();
		mTimer.scheduleAtFixedRate(new TimerTask()
		{
			@Override
			public void run() 
			{
				//[mo2sangbong.lee][1647] 2011.11.02 START
				time = ((SystemClock.elapsedRealtime()-mStartTime));
				//[mo2sangbong.lee][1647] 2011.11.02 END
				//[START]P930_TD#13400_20110720 Fixed issue after change settings date&time
				mHourStr = getTwoLengthNum(((time/1000)/60)/60);
				mMinuteStr = getTwoLengthNum((time/60000)%60);
				mSecondStr = getTwoLengthNum((time/1000)%60);
				mCentiSecondStr = Long.toString((time/100)%10);
				//[END]P930_TD#13400_20110720 Fixed issue after change settings date&time
	
					
				if(time >= 359999990)
				{
					stopTimer();
					//[START]P930_TD#13400_20110720 Fixed issue after change settings date&time
					time = 359999990;
					mHourStr = getTwoLengthNum(((time/1000)/60)/60);
					mMinuteStr = getTwoLengthNum((time/60000)%60);
					mSecondStr = getTwoLengthNum((time/1000)%60);
					mCentiSecondStr = Long.toString((time/100)%10);
					//[END]P930_TD#13400_20110720 Fixed issue after change settings date&time
				}
				//[START]P930_TD#13400_20110720 Fixed issue after change settings date&time
				else if(time < 0)
				{
					stopTimer();
					mHourStr = "00";
					mMinuteStr = "00";
					mSecondStr = "00";
					mCentiSecondStr = "00";
					saveIsStopWatchStartedPreferences(DEFAULT_BUTTON_STATE);
					//[hyunjung78.park] 2011.08.04 Apply GUI
					//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_rap);//P930_TD#13400_20110720 Fixed issue after change settings date&time
					setDefault();
					if (mWakeLock != null) 
					{
						mWakeLock.release();
						mWakeLock = null;
					}
				}
				//[END]P930_TD#13400_20110720 Fixed issue after change settings date&time
				updateStopWatchView();
			}
		}, 0, 100);
	}
	//[START]P930_TD#13400_20110720 Fixed issue after change settings date&time
	private void setDefault()
	{
		Runnable runnable = new Runnable()
		{
			public void run() 
			{			
				clearList();
				if(sw_lap_btn.isPressed()){
					sw_lap_btn.setTextColor(Color.BLACK);
				} else {
					sw_lap_btn.setTextColor(getResources().getColor(R.color.lap_button));	
				} 					
				setButtonState(DEFAULT_BUTTON_STATE);
			}
		};
		getActivity().runOnUiThread(runnable);	
	}
	//[END]P930_TD#13400_20110720 Fixed issue after change settings date&time
	private void stopTimer()
	{
		Log.d(TAG, "[stopTimer] : stop the stopwatch");
		//saveStartTimePreferences(0L);//P930_TD#13400_20110720 Fixed issue after change settings date&time
		if(mTimer != null)
		{
			mTimer.cancel();
		}
		bStartTimer = false;
	}
	
	private void updateStopWatchView()
	{
		Runnable runnable = new Runnable()
		{
			public void run() 
			{									
				mHourTextView.setText(mHourStr);
				mMinuteTextView.setText(mMinuteStr);
				mSecondTextView.setText(mSecondStr);
				mCentiSecondTextView.setText(mCentiSecondStr);
			}
		};
		getActivity().runOnUiThread(runnable);	
	}
	
	
	private void addList()
	{
		swAdapter.notifyDataSetChanged();
		if(LAPCOUNT > lap_list.size())
		{
			Log.d(TAG, "[addList] : add the laplist");
			lap_count++;
			
			LapValue lv = new LapValue(Integer.toString(lap_list.size()+1), mHourStr + ":" + mMinuteStr + ":" + mSecondStr, "."+mCentiSecondStr);
			
			lap_list.add(0, lv);
			swAdapter.notifyDataSetChanged();
		}
		else if(LAPCOUNT == lap_list.size())
		{
			sw_lap_btn.setEnabled(false); //seungchul.kang@lge.com	LapButton Dimmed
			sw_lap_btn.setTextColor(getResources().getColor(R.color.lap_button_disable));
			sw_lap_btn.setAlpha(0.4f);
			LapValue lv = new LapValue(Integer.toString(lap_list.size()+1), mHourStr + ":" + mMinuteStr + ":" + mSecondStr, "."+mCentiSecondStr);	
			lap_list.add(0, lv);
			
			Log.d(TAG, "[addList] : laplist is full");
			AlertDialog list_full_dialog = new AlertDialog.Builder(getActivity())
							.setTitle(R.string.sp_stopwatch_listFull_title_NORMAL)
							.setIconAttribute(android.R.attr.alertDialogIcon)						
							.setMessage(R.string.STR_Clock_SWLISTFULLMESSAGE_NORMAL)
							.setPositiveButton(R.string.timer_set_alerttone_ok, new DialogInterface.OnClickListener()
							{
								public void onClick(DialogInterface dialog,	int which) 
								{
									// sw_lap_btn.setClickable(true);
								}
							})
							.setCancelable(true).create();
			
			list_full_dialog.setCanceledOnTouchOutside(false);
			list_full_dialog.show();
		}
		sw_listview.requestFocus();
		sw_listview.setSelection(0);
	}
	
	private void clearList()
	{
		Log.d(TAG, "[clearList] : clear the laplist");
		lap_count = 0;
		// swDBAdapter.removeEntry();
		updateArray();
	}
	private int buttonstate;
	private void setButtonState(int btn_state)
	{
		switch(btn_state)
		{
			case DEFAULT_BUTTON_STATE :
			{
				buttonstate = DEFAULT_BUTTON_STATE;
				sw_reset_btn.setVisibility(View.GONE);
				sw_resume_btn.setVisibility(View.GONE);
				sw_stop_btn.setVisibility(View.GONE);//P930_TD#13400_20110720 Fixed issue after change settings date&time
				sw_lap_btn.setVisibility(View.GONE);//P930_TD#13400_20110720 Fixed issue after change settings date&time
				sw_start_btn.setVisibility(View.VISIBLE);
				sw_lap_btn.setEnabled(true); //seungchul.kang@lge.com	LapButton Active
				sw_lap_btn.setAlpha(1.0f);
				//[suhaeng.lee][6765] 2012.05.03 to use only blue background color
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_rap);
				break;
			}
			case START_BUTTON_STATE :
			{	
				buttonstate = START_BUTTON_STATE;
				sw_reset_btn.setVisibility(View.GONE);
				sw_resume_btn.setVisibility(View.GONE);
				sw_start_btn.setVisibility(View.GONE);
				sw_stop_btn.setVisibility(View.VISIBLE);
				sw_lap_btn.setVisibility(View.VISIBLE);
				//[suhaeng.lee][6765] 2012.05.03 to use only blue background color
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_start);
				break;
			}
			case STOP_BUTTON_STATE :
			{
				buttonstate = STOP_BUTTON_STATE;
				sw_start_btn.setVisibility(View.GONE);
				sw_stop_btn.setVisibility(View.GONE);
				sw_lap_btn.setVisibility(View.GONE);
				sw_reset_btn.setVisibility(View.VISIBLE);
				sw_resume_btn.setVisibility(View.VISIBLE);
				//[suhaeng.lee][6765] 2012.05.03 to use only blue background color
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_stop);
				break;
			}
			default :
			    break;
		}
	}
	
	// private int getButtonState()
	// {
	// return buttonstate;
	// }
	
	public void onClick(View v) 
	{	
		switch(v.getId())
		{	
			case R.id.sw_start_btn:
			{
				Log.d(TAG, "[onClick] : click the start button");
				//[mo2sangbong.lee][1647] 2011.11.02 START
				startTimer(SystemClock.elapsedRealtime());
				//[mo2sangbong.lee][1647] 2011.11.02 END
				saveIsStopWatchStartedPreferences(START_BUTTON_STATE);
				setButtonState(START_BUTTON_STATE);
				//[hyunjung78.park] 2011.08.04 Apply GUI
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_start);
				break;
			}
			case R.id.sw_stop_btn:
			{
				Log.d(TAG, "[onClick] : click the stop button");
				mFlowTime = time;
				stopTimer();
				saveStopTimePreferences(time);
				saveIsStopWatchStartedPreferences(STOP_BUTTON_STATE);
				setButtonState(STOP_BUTTON_STATE);
				//[hyunjung78.park] 2011.08.04 Apply GUI
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_stop);
				if (mWakeLock != null && mWakeLock.isHeld()) 
				{
					mWakeLock.release();
				}
				break;
			}
			case R.id.sw_lap_btn:
			{
				Log.d(TAG, "[onClick] : click the lap button");
				addList();
				break;
			}
			case R.id.sw_reset_btn:
			{
				Log.d(TAG, "[onClick] : click the reset button");
				stopTimer();
				clearList();
								
				mHourTextView.setText(R.string.sw_defaultvalue2);
				mMinuteTextView.setText(R.string.sw_defaultvalue2);
				mSecondTextView.setText(R.string.sw_defaultvalue2);
				mCentiSecondTextView.setText(R.string.sw_defaultvalue);
				saveIsStopWatchStartedPreferences(DEFAULT_BUTTON_STATE);
				//[hyunjung78.park] 2011.08.04 Apply GUI
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_rap);
				
				setButtonState(DEFAULT_BUTTON_STATE);
				if (mWakeLock != null && mWakeLock.isHeld()) 
				{
					mWakeLock.release();
				}

				break;
			}
			case R.id.sw_resume_btn:
			{
				Log.d(TAG, "[onClick] : click the resume button");
				/*//[hyunjung78.park] 2011.10.04 delete code for backlight on issue 
				//[hyunjung78.park][19119] 2011.08.05 Fixed issue that backlight is off START
				if(mWakeLock == null)
				{
					mWakeLock = mPm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "dim_Wake");
					mWakeLock.acquire();	
				}else{
					mWakeLock.acquire();
				}
				//[hyunjung78.park][19119] 2011.08.05 Fixed issue that backlight is off END
				*/
				//[mo2sangbong.lee][1647] 2011.11.02 START
				startTimer(SystemClock.elapsedRealtime() - mFlowTime);
				//[mo2sangbong.lee][1647] 2011.11.02 END
				saveIsStopWatchStartedPreferences(START_BUTTON_STATE);
				//[hyunjung78.park] 2011.08.04 Apply GUI
				//sw_bg_linearLayout.setBackgroundResource(R.drawable.stopwatch_bg_start);
				setButtonState(START_BUTTON_STATE);
				break;
			}
			default:
			{
				break;
			}
		}
	}
	//[mo2sangbong.lee][1647] 2011.11.02 START	
	private BroadcastReceiver mBroadCastReceiver = new BroadcastReceiver()
	{
		public void onReceive(Context context, Intent intent) 
		{
			if("android.intent.action.TIME_SET".equals(intent.getAction()))
			{
				int isStopWatchStarted = loadIsStopWatchStartedPreferences();
				Log.d(TAG,"onReceive loadIsStopWatchStartedPreferences : " + isStopWatchStarted);
				if((START_BUTTON_STATE == isStopWatchStarted) && bStartTimer)
				{
					Long time = loadStartTimePreferences();
					stopTimer();
					startTimer(time);
					setButtonState(START_BUTTON_STATE);
				}
			}

		}
	};
	//[mo2sangbong.lee][1647] 2011.11.02 END
	
	//[START]P930_TD#13400_20110720 Fixed issue after change settings date&time
	private String getTwoLengthNum(long nValue)
	{
		String sResult = "";
		
		if(10 > nValue)
		{
			sResult = "0" + nValue;
		}
		else
		{
			sResult = "" + nValue;
		}
		
		return sResult;
	}
	//[END]P930_TD#13400_20110720 Fixed issue after change settings date&time
	public static void saveIsStopWatchStartedPreferences(int stopwatchstate)
	{
		mEditor.putInt(STOPWATCHSTARTED, stopwatchstate);
		mEditor.commit();
	}
	
	public static int loadIsStopWatchStartedPreferences()
	{
		return mSharedPreferences.getInt(STOPWATCHSTARTED, -1);
	}
	
	public static void saveStartTimePreferences(Long starttime)
	{
		mEditor.putLong(STARTTIME, starttime);
		mEditor.commit();
	}
	
	public static Long loadStartTimePreferences()
	{
		return mSharedPreferences.getLong(STARTTIME, 0);
	}
	
	public static void saveStopTimePreferences(Long stoptime)
	{
		mEditor.putLong(ENDTIME, stoptime);
		mEditor.commit();
	}
	
	public static Long loadStopTimePreferences()
	{
		return mSharedPreferences.getLong(ENDTIME, 0);
	}
}